package com.zcb.agrimarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcb.agrimarket.mapper.ArrangeMapper;
import com.zcb.agrimarket.mapper.LabMapper;
import com.zcb.agrimarket.pojo.*;
import com.zcb.agrimarket.service.TeacherApplyLogService;
import com.zcb.agrimarket.mapper.TeacherApplyLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

/**
* @author KarrySmile
* @description 针对表【teacher_apply_log(教师申请表)】的数据库操作Service实现
* @createDate 2024-04-05 22:52:29
*/
@Service
public class TeacherApplyLogServiceImpl extends ServiceImpl<TeacherApplyLogMapper, TeacherApplyLog>
    implements TeacherApplyLogService{

    @Autowired
    LabMapper labMapper;

    @Autowired
    ArrangeMapper arrangeMapper;

    @Autowired
    TeacherApplyLogMapper teacherApplyLogMapper;

    @Override
    public TeacherApplyData selectList(TeacherApplyLog teacherApplyLog) {
        //用于记录可安排的节次
        int cnt=0;
        //查询有多少个教室
        ArrayList<Long> labIdList = labMapper.selectByLabtype(teacherApplyLog.getLabType());
        //查询所有可用教室的相关信息
        HashMap<Long,Lab> labHashMap = new HashMap<>();
        for (long labId : labIdList){
            labHashMap.put(labId,labMapper.selectById(labId));
        }

        //用于记录各个节次是否满足条件
        int[][] st = new int[6][7];
        //用于存放可以进行安排的课室
        ArrayList<Long>[][] labIds = new ArrayList[6][7];
        //遍历到的该节次：在规定周次内，有多少个教室可以安排，总容量是否超过所需人数。如果符合该节次可安排
        //周一到周五
        for(int day=1;day<=5;day++){
            //第一节次到第六节次
            for(int session=1;session<=6;session++){
                long number = 0;//该时间点所有教室所能容纳的学生人数
                //查看有多少个教室是空的（连续多周）
                for(long id:labIdList){
                    Arrange arrange = new Arrange();
                    for(int week=teacherApplyLog.getBeginWeek();week<=teacherApplyLog.getEndWeek();week++){
                         arrange = arrangeMapper.selectByMe(id,teacherApplyLog.getSemesterId(),week,day,session);
                         //在规定几周内如果课室被用了则说明该课室不可用
                         if(arrange!=null||(arrange!=null&&arrange.getStatus()!=0)) break;
                    }
                    //如果连续几周都可以，说明该课室可以用
                    if(arrange==null||(arrange!=null&&arrange.getStatus()!=0)) {
                        number = labHashMap.get(id).getDeviceNum() + number;
                        if(labIds[day][session]==null) labIds[day][session]=new ArrayList<>();
                        labIds[day][session].add(id);
                    }
                }
                //如果可用教室的总容量大于学生数量，说明该节次可以用
                if(number>=teacherApplyLog.getStudentNum()){
                    st[day][session]=1;
                    cnt++;
                }
            }
        }
        int[][] data = new int[cnt][2];
        int index = 0;
        for(int day=1;day<=5;day++){
            for(int session=1;session<=6;session++){
                if(st[day][session]==1){
                    data[index][0]=day;
                    data[index][1]=session;
                    index++;
                }
            }
        }
        TeacherApplyData teacherApplyData = new TeacherApplyData();
        teacherApplyData.setSession(data);
        teacherApplyData.setLabIds(labIds);
        return teacherApplyData;
    }

    @Override
    public void apply(TeacherApplyData teacherApplyData) {

    }

    /**
     * 教师提交申请，如果可以返回并安排添加记录
     * @param teacherApplyLog
     * @return
     */
    @Override
    public Boolean check(TeacherApplyLog teacherApplyLog) {
        //用于记录可安排的节次
        int cnt=0;
        //查询有多少个教室
        ArrayList<Long> labIdList = labMapper.selectByLabtype(teacherApplyLog.getLabType());
        //查询所有可用教室的相关信息
        HashMap<Long,Lab> labHashMap = new HashMap<>();
        for (long labId : labIdList){
            labHashMap.put(labId,labMapper.selectById(labId));
        }

        //用于记录各个节次是否满足条件
        int[][] st = new int[6][7];
        //用于存放可以进行安排的课室
        ArrayList<Long>[][] labIds = new ArrayList[6][7];
        //遍历到的该节次：在规定周次内，有多少个教室可以安排，总容量是否超过所需人数。如果符合该节次可安排
        //周一到周五
        for(int day=1;day<=5;day++){
            //第一节次到第六节次
            for(int session=1;session<=6;session++){
                long number = 0;//该时间点所有教室所能容纳的学生人数
                //查看有多少个教室是空的（连续多周）
                for(long id:labIdList){
                    Arrange arrange = new Arrange();
                    for(int week=teacherApplyLog.getBeginWeek();week<=teacherApplyLog.getEndWeek();week++){
                        arrange = arrangeMapper.selectByMe(id,teacherApplyLog.getSemesterId(),week,day,session);
                        //在规定几周内如果课室被用了则说明该课室不可用
                        if(arrange!=null||(arrange!=null&&arrange.getStatus()!=0)) break;
                    }
                    //如果连续几周都可以，说明该课室可以用
                    if(arrange==null||(arrange!=null&&arrange.getStatus()!=0)) {
                        number = labHashMap.get(id).getDeviceNum() + number;
                        if(labIds[day][session]==null) labIds[day][session]=new ArrayList<>();
                        labIds[day][session].add(id);
                    }
                }
                //如果可用教室的总容量大于学生数量，说明该节次可以用
                if(number>=teacherApplyLog.getStudentNum()){
                    st[day][session]=1;
                    cnt++;
                }
            }
        }
        if(cnt>=teacherApplyLog.getAmountOfSession()){
            teacherApplyLogMapper.insert(teacherApplyLog);
            return true;
        }
        else return false;
    }

    @Override
    public void updateById(Long id, Integer state) {
        teacherApplyLogMapper.updateById(id,state);
    }

}